package com.tutu.user.service.impl;

import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tutu.common.GlobalConstants;
import com.tutu.user.dao.SysMenuInfoMapper;
import com.tutu.user.entity.SysMenuInfo;
import com.tutu.user.entity.SysMenuInfoExample;
import com.tutu.user.entity.SysMenuInfoExample.Criteria;
import com.tutu.user.service.ISysMenuInfoService;

@Service
public class SysMenuInfoServiceImpl implements ISysMenuInfoService {
	@Resource
	private SysMenuInfoMapper sysMenuInfoMapper;

	@Override
	public Set<SysMenuInfo> getSysMenuInfoByUserId(String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(userId))
			map.put("userId", userId);
		map.put("aliveFlag", GlobalConstants.FLAG_ENABLE);
		return new HashSet<SysMenuInfo>(sysMenuInfoMapper.getSysMenuInfoByUserId(map));
	}

	@Override
	public List<SysMenuInfo> getSysMenuInfos(SysMenuInfo sysMenuInfo) {
		SysMenuInfoExample sysMenuInfoExample = new SysMenuInfoExample();
		Criteria criteria = sysMenuInfoExample.createCriteria();
		if(StringUtils.isNotBlank(sysMenuInfo.getAliveFlag()))
			criteria.andAliveFlagEqualTo(sysMenuInfo.getAliveFlag());
		return this.sysMenuInfoMapper.selectByExample(sysMenuInfoExample);
	}

	@Override
	public void saveSysMenuInfo(SysMenuInfo sysMenuInfo) throws Exception {
		this.sysMenuInfoMapper.insert(sysMenuInfo);
	}

	@Override
	public SysMenuInfo getSysMenuInfo(String menuId) throws Exception {
		return this.sysMenuInfoMapper.selectByPrimaryKey(menuId);
	}

	@Override
	public void updateSysMenuInfo(SysMenuInfo sysMenuInfo) throws Exception {
		sysMenuInfo.setModifyTime(Calendar.getInstance().getTime());
		this.sysMenuInfoMapper.updateByPrimaryKeySelective(sysMenuInfo);
	}
	
}
